const { ccclass, property } = cc._decorator;
import MonsterControl from '../Monster/MonsterControl';
import AttackManager from '../SkillAttack/AttackManager';
import MyEnums from '../MyEnums';
import TargetBase from '../SkillAttack/TargetBase';

@ccclass
export default class SkillStrike extends cc.Component {//突进
    @property(cc.Node)
    strikeSprite: cc.Node = null;
    @property(cc.Node)
    _MonsterBody: cc.Node = null;
    @property
    speed: number = 100;
    @property
    interval: number = 20;//释放间隔
    @property
    addDamage: number = 0;
    @property
    desArmor: number = 0;
    @property(cc.Node)
    _AttacksNode: cc.Node = null;
    @property
    _controlAttack: AttackManager = null;

    reindex: number = 2;
    Attacked: string[] = [];//已经输出过伤害的节点ID，防重复输出
    _MonsterControl: MonsterControl = null;
    _collider: cc.CircleCollider = null;
    onLoad() {
        this._MonsterControl = this._MonsterBody.getComponent(MonsterControl);
        this._collider = this.node.getComponent(cc.CircleCollider);
        this._collider.enabled = false;
        this.strikeSprite.active = false;
        this.strikeSprite.opacity = 30;
        this.BuildAttack();
        this.schedule(this.doSkill, this.interval);
    }
    onCollisionEnter(other, self) {
        if (this.Attacked.indexOf(other.node.uuid) > -1)
            return;
        if (other.tag == TagHeroBody) {
            this.doAttack(other.node);
        }
    }
    doAttack(target: cc.Node) {
        this._controlAttack.damageAttack = this._MonsterControl.attackDamage;
        let _TargetBase = target.parent.parent.getComponent(TargetBase);
        this._controlAttack.doAttack(target, MyEnums.SkillSource.hero, _TargetBase);
    }
    doSkill() {
        if (this._MonsterControl.checkBusy() && this.reindex > 0) {
            this.scheduleOnce(() => {
                this.reindex--;
                this.doSkill();
            }, 1);
            return;
        }
        else {
            let heroP = HeroHelper.getInstance().GetWorldPosition();
            let thisP = this.strikeSprite.convertToWorldSpaceAR(cc.v2(0, 0));
            let faceTo = 1;
            if (heroP) {
                let Px = heroP.x - thisP.x;
                if (Px < 0)
                    faceTo = -1;
            }
            this._MonsterControl.setBusy();
            this._MonsterControl.setFaceto(faceTo);
            this.reindex = 3;
            let lv = this._MonsterControl._Rbody.linearVelocity;
            lv.x += this.speed * faceTo;
            this._MonsterControl._Rbody.linearVelocity = lv;
            this.strikeSprite.active = true;
            this._collider.enabled = true;
            this.setTween();
        }
    }
    setTween() {
        let self = this;
        cc.tween(this.strikeSprite)
            .to(0.5, { opacity: 200 })
            .to(0.5, { opacity: 30 })
            .call(() => {
                self._collider.enabled = false;
                self.strikeSprite.active = false;
                self._MonsterControl.setNotBusy();
            })
            .start();
    }
    private BuildAttack() {
        this._AttacksNode = new cc.Node();
        this._controlAttack = this._AttacksNode.addComponent(AttackManager);
        this._controlAttack.addRefresItem(MyEnums.SkillRefreshType.life, this.addDamage);
        this._controlAttack.addRefresItem(MyEnums.SkillRefreshType.armorC, this.desArmor);
        this._controlAttack.addRefresItem(MyEnums.SkillRefreshType.armorA, this.desArmor);
        this._controlAttack.attribute = this._MonsterControl.attribute;
        this._controlAttack.source = MyEnums.SkillSource.monster;
        this.node.addChild(this._AttacksNode);
    }
}
